package com.zdft.promotion.common.dao;


import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.zdft.promotion.common.bean.AbstractBean;
import com.zdft.promotion.common.model.AbstractFilterModel;

/**
 * 默认DAO实现
 */
public abstract class DefaultDAO<PK extends Serializable, T extends AbstractBean> extends AbstractDAO
{
    public DefaultDAO()
    {
        this(true);
    }

    public DefaultDAO(boolean namespaceEnabled)
    {
        super(namespaceEnabled);
    }

    public DefaultDAO(boolean namespaceEnabled, int batchSize)
    {
        super(namespaceEnabled, batchSize);
    }

    public int insert(T bean)
    {
        bean.setCreateTime(new Date());
        return insert("insert", bean);
    }

    public void batchInsert(List<T> beans)
    {
        for (T bean : beans)
        {
            bean.setCreateTime(new Date());
        }
        batchInsert("insert", beans);
    }

    public int delete(PK pk)
    {
        return update("delete", pk);
    }

    public void batchDelete(List<PK> pkList)
    {
        batchUpdate("delete", pkList);
    }

    public int update(T bean)
    {
        bean.setUpdateTime(new Date());
        return update("update", bean);
    }

    public void batchUpdate(List<T> beans)
    {
        for (T bean : beans)
        {
            bean.setUpdateTime(new Date());
        }
        batchUpdate("update", beans);
    }

    public T getByPrimaryKey(PK pk)
    {
        return selectOne("getByPrimaryKey", pk);
    }

    public long count(T arg)
    {
        return selectOne("count", arg);
    }

    public List<T> list(T arg)
    {
        return selectList("list", arg);
    }

    public long filterCount(AbstractFilterModel filter)
    {
        return selectOne("filterCount", filter);
    }

    public List<T> filterList(AbstractFilterModel filter)
    {
        return selectList("filterList", filter);
    }
}